﻿using Microsoft.AspNetCore.SignalR;
using MultiAgentsServer.Scripts.Services.DataModels;
using MultiAgentsShared;

namespace MultiAgentsServer.Scripts.Hubs;

public class SignalRUserSettingsHub(IEventBus eventBus) : SignalRBaseHub(eventBus)
{
    #region ApiRequest
    public void SendSessionId(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");

        _eventBus.Publish(new SaveSessionIdCommand(Context.ConnectionId, sessionId, userId, UserCookieDataModel.HubConnectionClassEnum.UserSettings));
    }

    public void ArchiveChatRoomApiRequest(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");
        var chatRoomId = requestParams.GetData<int>("chatRoomId");

        _eventBus.Publish(new ArchiveChatRoomCommand(Context.ConnectionId, sessionId, userId, chatRoomId));
    }

    public void DeleteChatRoomApiRequest(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");
        var chatRoomId = requestParams.GetData<int>("chatRoomId");

        _eventBus.Publish(new DeleteChatRoomCommand(Context.ConnectionId, sessionId, userId, chatRoomId));
    }

    public void RenameChatRoomApiRequest(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");
        var chatRoomId = requestParams.GetData<int>("chatRoomId");
        var chatRoomIName = requestParams.GetData<string>("chatRoomIName");

        _eventBus.Publish(new RenameChatRoomCommand(Context.ConnectionId, sessionId, userId, chatRoomId, chatRoomIName));
    }

    public void FetchProjectSettingsApiRequest(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");
        var projectId = requestParams.GetData<int>("projectId");

        _eventBus.Publish(new FetchProjectSettingsCommnad(Context.ConnectionId, sessionId, userId, projectId));
    }

    public void SaveProjectSettingsApiRequest(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");
        var projectId = requestParams.GetData<int>("projectId");
        var projectSettings = requestParams.GetData<string>("projectSettings");

        _eventBus.Publish(new SaveProjectSettingsCommnad(Context.ConnectionId, sessionId, userId, projectId, projectSettings));
    }

    public void AllowToolCallChangedApiRequest(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");
        var isAllow = requestParams.GetData<bool>("isAllow");

        _eventBus.Publish(new AllowToolCallChangedCommnad(Context.ConnectionId, sessionId, userId, isAllow));
    }
    #endregion

    #region ApiResponse
    public static async Task ArchiveChatRoomApiResponse(IHubContext<SignalRUserSettingsHub> hubContext, string connectionId, int chatRoomId, int projectId)
    {
        await hubContext.Clients.Client(connectionId).SendAsync("ArchiveChatRoomApiResponse", chatRoomId, projectId);
    }

    public static async Task DeleteChatRoomApiResponse(IHubContext<SignalRUserSettingsHub> hubContext, string connectionId, int chatRoomId, int projectId)
    {
        await hubContext.Clients.Client(connectionId).SendAsync("DeleteChatRoomApiResponse", chatRoomId, projectId);
    }

    public static async Task RenameChatRoomApiResponse(IHubContext<SignalRUserSettingsHub> hubContext, string connectionId, int prjId, int chatRoomId, string chatRoomName)
    {
        await hubContext.Clients.Client(connectionId).SendAsync("RenameChatRoomApiResponse", prjId, chatRoomId, chatRoomName);
    }

    public static async Task FetchProjectSettingsApiResponse(IHubContext<SignalRUserSettingsHub> hubContext, string connectionId, int prjId, string? prjSettings)
    {
        await hubContext.Clients.Client(connectionId).SendAsync("FetchProjectSettingsApiResponse", prjId, prjSettings);
    }

    public static async Task SaveProjectSettingsResponse(IHubContext<SignalRUserSettingsHub> hubContext, string connectionId)
    {
        await hubContext.Clients.Client(connectionId).SendAsync("SaveProjectSettingsResponse");
    }

    public static async Task AllowToolCallChangedResponse(IHubContext<SignalRUserSettingsHub> hubContext, string connectionId, bool isAllow)
    {
        await hubContext.Clients.Client(connectionId).SendAsync("AllowToolCallChangedResponse", isAllow);
    }
    #endregion

    #region ConnectionRelated
    // This method is called whenever a client disconnects from the hub
    public override Task OnDisconnectedAsync(Exception? exception)
    {
        _eventBus.Publish(new RemoveConnectionCommand(Context.ConnectionId));
        return base.OnDisconnectedAsync(exception);
    }
    #endregion
}
